package com.abyss;


import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;

import java.io.File;
import java.io.IOException;

/**
 * @Date 2020/9/12
 */
public class AvroTest {

    public static void main(String[] args) throws IOException {
        /**
         * 开发步骤
         * 1.构建对象
         * 2.封装数据
         * 3.序列化操作
         * 4.反序列化操作
         */

        //1.构建对象
        User user = new User();
        //2.封装数据
        user.setName("唐三");
        user.setAge(15);
        user.setAddress("圣魂村");
        User user1 = new User("小舞", 15, "星斗大森林");
        User user2 = User.newBuilder()
                .setName("唐昊")
                .setAge(40)
                .setAddress("昊天宗")
                .build();

        //3.序列化操作
        SpecificDatumWriter<User> datumWriter = new SpecificDatumWriter<>(user.getSchema());
        DataFileWriter<User> fileWriter = new DataFileWriter<>(datumWriter);
        fileWriter.create(user.getSchema(),new File("avro.txt"));
        fileWriter.append(user);
        fileWriter.append(user1);
        fileWriter.append(user2);
        fileWriter.close();


        //4.反序列化操作
        SpecificDatumReader<User> datumReader = new SpecificDatumReader<User>(User.class);
        DataFileReader<User> fileReader = new DataFileReader<User>(new File("avro.txt"), datumReader);
        while (fileReader.hasNext()){
            System.out.println(fileReader.next());
        }
        fileReader.close();
    }
}